package mamikeji.liaowa.ui.activity.notice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.PushMessage;
import mamikeji.liaowa.bean.UserNoticeBean;
import mamikeji.liaowa.db.PushMessageManager;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.ui.adapter.UserNoticeAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.widget.slidelistview.SilderListView;

/**
 *
 */
public class UserNoticeActivity extends BaseActivity {
    private static final String TAG = "UserNoticeActivity";
    private ListView listView_user_notice;
    private UserNoticeAdapter userNoticeAdapter;
    private List<UserNoticeBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_notice);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.user_notice_name);
        init();
    }

    /**
     * 加载布局控件
     */
    private void init() {
        list = new ArrayList<>();
        list.add(new UserNoticeBean(R.drawable.icon_xinhuifu_n, "新回复通知", "", "您的...帖子收到一条回复", 0));
        list.add(new UserNoticeBean(R.drawable.icon_huodongtixing_n, "活动提醒", "", "您参与的活动即将开始，快去看看吧！", 0));
        list.add(new UserNoticeBean(R.drawable.icon_meiriyesuan_n, "系统消息", "", "你有新消息推送，快来看看吧", 0));

        userNoticeAdapter = new UserNoticeAdapter(this, list);
        listView_user_notice = (SilderListView) findViewById(R.id.listView_user_notice);

        listView_user_notice.setAdapter(userNoticeAdapter);

        userNoticeAdapter.setOnItemClickListener(new UserNoticeAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(int id) {
                list.remove(id);
                userNoticeAdapter.notifyDataSetChanged();
            }
        });

        listView_user_notice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent mIntent = null;
                switch (i) {
                    case 0:
                        mIntent = new Intent(UserNoticeActivity.this, Notice_ReplyActivity.class);
                        break;
                    case 1:
                        mIntent = new Intent(UserNoticeActivity.this, Notice_EventActivity.class);
                        break;
                    case 2:
                        mIntent = new Intent(UserNoticeActivity.this, Notice_SystemActivity.class);
                        break;
                }
                if (mIntent != null) {
                    startActivity(mIntent);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
        getSystemUnReadMessageAndTime();
    }

    private void getSystemUnReadMessageAndTime() {
        int unReadMessage = PushMessageManager.getUnReadMessage();
        List<PushMessage> pushMessageList = PushMessageManager.queryAll();
        UserNoticeBean itemNotice = (UserNoticeBean) userNoticeAdapter.getItem(2);
        if(!pushMessageList.isEmpty()){
            itemNotice.setTime(pushMessageList.get(pushMessageList.size()-1).getTime());
        }else{
            itemNotice.setTime("");
        }
        itemNotice.setUnreadMsg(unReadMessage);
        System.out.println("unReadMessage:" + unReadMessage);
        userNoticeAdapter.notifyDataSetChanged();
    }

    private void getData() {
        Map<String, String> formMap = new HashMap<>();
        formMap.put("pageindex", "1");
        formMap.put("pagesize", "10");
        formMap.put("usercode", ForPreference.getString(ForContents.USER_CODE));
        ForHttp.setMsg(HttpApi.ROOT_PATH + "api/Message/GetNotMessageCount", formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
            }

            @Override
            public void onResponse(String response) {
                System.out.println("未读消息：" + response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if ("200".equals(jsonObject.getString(HttpKey.STATUS))) {
                        JSONObject object = jsonObject.getJSONObject("data");
                        int praise = object.getInt("praise");
                        int comments = object.getInt("comments");
                        UserNoticeBean itemNotice = (UserNoticeBean) userNoticeAdapter.getItem(0);
                        itemNotice.setUnreadMsg(praise + comments);
                        userNoticeAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
